package shellstation;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import java.sql.ResultSet;

public class UpdateProductView extends JFrame implements ActionListener {

   
    private int k;

    private JPanel panel;
    
    private JButton submit;
    private JButton searchButton; 
    private JButton viewAllButton;
    
    private JScrollPane scrollpane2;
    
    private DefaultTableModel listOfP;

    private JTextField searchField;
    
    private ImageIcon searchIcon;

    private JComboBox box;

    private JLabel name;
    private JLabel filter;
    private JLabel search;

    private ArrayList listSize = new ArrayList();
  
    private JTable productTable;
    private    String[] productColumn = {"Product Name", "Status", "Size","Price","Stock"};
    
    public void UpdateProductView(){

        searchIcon= new ImageIcon(getClass().getResource("image/search.gif"));
        
        ResultSet rs;
        
        ProductModel pr = new ProductModel();
        listSize = pr.getlistSize();
        
        ProductModel rm = new ProductModel();
            //ArrayList al= new ArrayList();
        rs = rm.getAllProduct();
        
        try{
        while( rs.next())
            k++;
        } catch(Exception ex){}
        
        submit=new JButton("Update");
        submit.setBounds(320, 340, 100, 25 );
        submit.setFont(new Font("Hobo Std", Font.BOLD, 15));
        submit.addActionListener(this);
        
        filter = new JLabel("Filter by size:");
        filter.setBounds(20, 20, 200, 25 );
        filter.setFont(new Font("Hobo Std", Font.BOLD, 15));
        
        box = new JComboBox(listSize.toArray());
        box.setBounds(150, 23, 180, 25);
        
        search = new JLabel("Enter name:");
        search.setBounds(20, 70, 200, 25);
        search.setFont(new Font("Hobo Std", Font.BOLD, 15)); 
        
        searchField = new JTextField("",10);
        searchField.setBounds(150, 70, 180, 25 );
        searchField.setFont(new Font("Hobo Std", Font.BOLD, 15));
        
        searchButton= new JButton("",searchIcon);
        searchButton.setBounds(350,70,25,25);
        searchButton.setBackground(null);
        searchButton.addActionListener(this);
        
        listOfP = new DefaultTableModel(k,1);
        listOfP.setColumnIdentifiers(productColumn);


        productTable = new JTable(listOfP){

            public boolean isCellEditable(int i, int columnNames){
            i++;
            return false;
        }};
        productTable.setPreferredScrollableViewportSize(new Dimension(400, 300));
        
        scrollpane2= new JScrollPane(productTable);
        scrollpane2.setBounds(20, 120, 400, 200);
        
        viewAllButton=new JButton("View All");
        viewAllButton.setBounds(20, 340, 100, 25 );
        viewAllButton.setFont(new Font("Hobo Std", Font.BOLD, 15));
        viewAllButton.addActionListener(this);
        
        /*productTable = new JTable(ProductRow, ProductCol){
                    public boolean isCellEditable(int ProductRow, int ProductCol){
                        return false;
                        }};
				
        //productTable.setFont(new Font("Arial", Font.BOLD, 20));
       
                                productTable.setBounds(20, 120, 400, 300);
                                scrollPanel = new JScrollPane(productTable);
                                scrollPanel.setBounds(23, 120, 400, 200);
                                productTable.setFillsViewportHeight(true);

        /*panel=new JPanel(null);
        panel.setBounds(300, 0, 160, 424);
        //panel.setBackground(Color.LIGHT_GRAY);

        panel.add(submit);*/

    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource()== submit){
        
         
         int b=productTable.getSelectedRow();
         System.out.println(productTable.getValueAt(b, 2));
         try {
         if(productTable.getValueAt(b, 2) != " "){
             
         String product = productTable.getValueAt(b, 0).toString();
         String size = productTable.getValueAt(b, 2).toString();
         
         ProductController pc = new ProductController();
         //ID.add(temp.get(0));
         UpdateProductFormView uf = new UpdateProductFormView();
         uf.UpdateProductFormView(pc.getProductID(product, size));
         uf.setPanel(panel);
         panel.removeAll();
         panel.repaint();
         panel.add(uf.getStatus());
         panel.add(uf.getProductName());
         panel.add(uf.getProductPrice());
         panel.add(uf.getProductStock());
         panel.add(uf.getProductStockTotal());
         panel.add(uf.getpName());
         panel.add(uf.getbox2());
         panel.add(uf.getpPrice());
         panel.add(uf.getpStock());
         panel.add(uf.getsubmit());
         }
         else
           JOptionPane.showMessageDialog(null, "Nothing to update.");
         } catch(NullPointerException ex){
            JOptionPane.showMessageDialog(null, "Nothing to update.");}
        }
            
        
        else if( e.getSource() == searchButton){
            int a=0;
            ResultSet rs;
            int i = 0;
            int j = 0;
            while(a < productTable.getRowCount()){
                  productTable.setValueAt(" ", a, 0);
                  productTable.setValueAt(" ", a, 1);
                  productTable.setValueAt(" ", a, 2);
                  productTable.setValueAt(" ", a, 3);
                  productTable.setValueAt(" ", a, 4);
                  a++;
            }
            
            String s = searchField.getText();
            //ArrayList al = new ArrayList();
            if(s.isEmpty()){
            ProductModel rm = new ProductModel();
            //ArrayList al= new ArrayList();
            rs = rm.getProduct(box.getSelectedItem().toString());
            }
            
            else {
            ProductModel rm = new ProductModel();
            //ArrayList al= new ArrayList();
            rs = rm.getProductTable(s);
            if(rs == null) {
                JOptionPane.showMessageDialog(null, "Product does not exist!");
                searchField.setText("");
            }
            }
                
             try{
                while(rs.next()){

                    productTable.setValueAt(rs.getString("productName"), i, 0);
                    productTable.setValueAt(rs.getString("status"), i, 1);
                    productTable.setValueAt(rs.getString("size"), i, 2);
                    productTable.setValueAt(rs.getString("price"), i, 3);
                    productTable.setValueAt(rs.getString("stock"), i, 4);
                    i++;
                }
            }

            catch(Exception ex){
                ex.printStackTrace();
            }
           
        }
        else if( e.getSource() == viewAllButton){
            int a=0;
            ResultSet rs;
            int i = 0;
            int j = 0;
            while(a < productTable.getRowCount()){
                  productTable.setValueAt(" ", a, 0);
                  productTable.setValueAt(" ", a, 1);
                  productTable.setValueAt(" ", a, 2);
                  productTable.setValueAt(" ", a, 3);
                  productTable.setValueAt(" ", a, 4);
                  a++;
            }
            
            
            ProductModel rm = new ProductModel();
            //ArrayList al= new ArrayList();
            rs = rm.getAllProduct();
            
                
             try{
                while(rs.next()){

                    productTable.setValueAt(rs.getString("productName"), i, 0);
                    productTable.setValueAt(rs.getString("status"), i, 1);
                    productTable.setValueAt(rs.getString("size"), i, 2);
                    productTable.setValueAt(rs.getString("price"), i, 3);
                    productTable.setValueAt(rs.getString("stock"), i, 4);
                    i++;
                }
            }

            catch(Exception ex){
                ex.printStackTrace();
            }
           
        }
    
    }

    public ImageIcon getSearchIcon(){
        return searchIcon;
    }
    
   public JTable getproductTable(){
        return productTable;
    }

    public JScrollPane getscrollPane2(){
        return scrollpane2;
    }

    public JButton getsubmit(){
        return submit;
    }
    
    public JButton getSearchButton(){
        return searchButton;
    }

    public JButton getViewAllButton(){
        return viewAllButton;
    }
    
    public JLabel getFilter(){
        return filter;
    }

    public JComboBox getBox(){
        return box;
    }

    public JLabel getSearch(){
        return search;
    }

    public JTextField getSearchField(){
        return searchField;
    }
    
    public void setPanel(JPanel panel2)
    {panel = panel2; }

}
